package cst.a706gdy.zyfypt_706gdy.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import cst.a706gdy.zyfypt_706gdy.R;
import cst.a706gdy.zyfypt_706gdy.listener.AmicoolListener;
import cst.a706gdy.zyfypt_706gdy.model.ResModel;

public class RegActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private EditText email;
    private EditText tel;
    private EditText realname;
    private Button btn_reg;
    private Button btn_return;
    private AmicoolListener<String> listener;
    private ResModel model;
    private Intent intent;
    private RadioButton btn_student;
    private EditText re_password;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg2);
        username=(EditText)findViewById(R.id.reg_username);
        password=(EditText)findViewById(R.id.reg_password);
        email=(EditText)findViewById(R.id.reg_email);
        tel=(EditText)findViewById(R.id.reg_tel);
       realname=(EditText)findViewById(R.id.reg_realname);
        btn_reg=(Button)findViewById(R.id.btn_reg );
        btn_return=(Button)findViewById(R.id.btn_return);
        btn_student=(RadioButton)findViewById(R.id.btn_student);
        re_password=(EditText)findViewById(R.id.reg_re_password);

        intent=new Intent(RegActivity.this,LoginActivity.class);

        listener=new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if (l.equals("1")) {
                    Toast.makeText(RegActivity.this, "注册成功，返回登录", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    finish();
                }
                else
                    Toast.makeText(RegActivity.this, "用户已经存在", Toast.LENGTH_SHORT).show();

            }
            @Override
            public void onFail(String msg) {
               Toast.makeText(RegActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        };
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(password.getText().toString().equals(re_password.getText().toString())){
                model=new ResModel("scalar");
                if(btn_student.isChecked())
                model.reg(username.getText().toString(),realname.getText().toString(),2,password.getText().toString(),re_password.getText().toString(),
                        email.getText().toString(),tel.getText().toString(),
                        listener);
                else

                    model.reg(username.getText().toString(),realname.getText().toString(),3,password.getText().toString(),re_password.getText().toString(),
                            email.getText().toString(),tel.getText().toString(),
                            listener);}
                else{
                    Toast.makeText(RegActivity.this,"两次密码不一样",Toast.LENGTH_SHORT).show();
                }

            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(intent);
                finish();
            }
        });
    }
}
